using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Soft1ApiDemo.Domain.Repository;
using Soft1ApiDemo.Infrastructure.Repository;
using Soft1ApiDemo.Infrastructure.Db;
using Microsoft.EntityFrameworkCore;
using Soft1ApiDemo.Infrastructure.Filters;

namespace Soft1ApiDemo.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var _conString=Configuration.GetConnectionString("SqlServer");
            services.AddDbContext<Soft1ApiDemoDbContext>(options=>options.UseSqlServer(_conString));

            // 依赖注入 将CRUD的实现类注册到接口
            services.AddScoped(typeof(IRepository<>),typeof(RepositoryBase<>));
            // services.AddConnections();
            services.AddControllers(options=>options.Filters.Add(new CustomGlobalExceptionFilter()));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
